package com.example.mallproduct.controller;

import com.common.utils.PageUtils;
import com.common.utils.R;
import com.example.mallproduct.entity.BrandEntity;
import com.example.mallproduct.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 品牌
 *
 * @author charlie
 * @email @gmail.com
 * @date 2023-11-26 14:55:58
 */
@RestController
@RequestMapping("mallproduct/brand")
@RefreshScope
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    public R save(@Valid @RequestBody BrandEntity brand){
    public R save(@Validated(BrandEntity.AddBrand.class) @RequestBody BrandEntity brand){


//        if (bindingResult.hasErrors()){
//            Map map=new HashMap<>();
//            for (FieldError fieldError : bindingResult.getFieldErrors()) {
//
//                map.put(fieldError.getField(),fieldError.getDefaultMessage());
//            }
//            return R.ok().put("data",map);
//        }

		brandService.save(brand);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody BrandEntity brand){
		brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }
    @RequestMapping("/brand-list")
    public R brandList(){
        List<BrandEntity> page = brandService.list();

        return R.ok().put("page", page);
    }

}
